package com.aji.biomarker.controller;

import com.aji.allcommon.result.Result;
import com.aji.biomarker.domain.Reference;
import com.aji.biomarker.domain.dto.ReferenceDto;
import com.aji.biomarker.service.ReferenceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "文献管理相关接口")
@RestController
@RequestMapping("/ref")
@RequiredArgsConstructor
@CrossOrigin
public class ReferenceController {
    private final ReferenceService referenceService;

    @ApiOperation("增加 需要忽略id")
    @PostMapping()
    public Result save(@RequestBody Reference entity){
        referenceService.save(entity);
        return Result.success();
    }
    @ApiOperation("删除")
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        referenceService.removeByIds(ids);
        return Result.success();
    }
    @ApiOperation("修改")
    @PutMapping()
    public Result updateById(@RequestBody Reference entity){
        System.out.println(entity);
        referenceService.updateById(entity);
        return Result.success();
    }
    @ApiOperation("查询 根据id")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.success(referenceService.getById(id));
    }

    @ApiOperation("查询 全部")
    @GetMapping("/list")
    public Result list(){
        QueryWrapper<Reference> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT reference", "id"); // 只去重 reference 字段

        List<Reference> list = referenceService.list(wrapper);

        return Result.success(list);
    }

    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{pageSize}")
    public Result listByConditionAndPage(@PathVariable int page, @PathVariable int pageSize, String reference){
        if (page < 1 || pageSize < 1) {
            return Result.error("分页参数异常");
        }

        // 构造查询条件
        QueryWrapper<Reference> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT reference"); // 只返回 reference 字段去重
        if (reference != null && !reference.isEmpty()) {
            wrapper.like("reference", reference);
        }

        // 分页查询
        Page<Reference> referencePage = new Page<>(page, pageSize);
        IPage<Reference> referenceIPage = referenceService.page(referencePage, wrapper);

        return Result.success(referenceIPage);
    }


    @ApiOperation("数据统计")
    @GetMapping("/count")
    public Result<List<ReferenceDto>> getOriginCounts() {
        try {
            List<ReferenceDto> counts = referenceService.countByRefer();
            return Result.success(counts);
        } catch (Exception e) {
            return Result.error("获取数据统计失败: " + e.getMessage());
        }
    }
}
